//
//  ZWLunBoView.m
//  WanEr
//
//  Created by 高文萍 on 15/12/21.
//  Copyright © 2015年 高文萍. All rights reserved.
//

#import "ZWLunBoView.h"
#import "ZWLunBoCell.h"
#import "ZWLuoBoModel.h"
@implementation ZWLunBoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
        [self createPageControl];
    }
    return self;
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, WIDTH);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH) collectionViewLayout:flowLayout];
    [self addSubview:self.collectionView];
    
    [self.collectionView registerClass:[ZWLunBoCell class] forCellWithReuseIdentifier:@"collection"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
}

- (void)createPageControl
{
    self.pageControl = [[UIPageControl alloc] init];
    [self addSubview:self.pageControl];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@((WIDTH - 200) / 2));
        make.top.equalTo(@(WIDTH - 20));
        make.width.equalTo(@200);
        make.height.equalTo(@20);
        
    }];
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.798 green:0.534 blue:0.276 alpha:1.000];
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    self.pageControl.currentPage = 0;
    [self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)setScrollArr:(NSMutableArray *)scrollArr
{
    if (_scrollArr != scrollArr) {
        _scrollArr = scrollArr;
    }
    [self.collectionView reloadData];
    self.pageControl.numberOfPages = self.scrollArr.count - 2;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.scrollArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZWLunBoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    
    NSMutableArray *arr = [NSMutableArray array];
    arr = self.scrollArr;
    [cell.myImg sd_setImageWithURL:[NSURL URLWithString:arr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"占位图01"]];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

#pragma --- 点击轮播图 ---
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    self.lunBoBlock(indexPath.row);
//}

#pragma --- 点击pageControl,让图片跟着动 ---
- (void)pageControlAction:(UIPageControl *)pageControl
{
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.pageControl.currentPage + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
}

#pragma --- 手动滑动collection可以循环, 并让page 跟着动 ---
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.collectionView.contentOffset.x == 0) {
        
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.scrollArr.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        
    }else if (self.collectionView.contentOffset.x == WIDTHVIEW * (self.scrollArr.count - 1)){
        
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    }
    
    self.pageControl.currentPage = self.collectionView.contentOffset.x / WIDTHVIEW - 1;
    
}


#pragma ---- 开始拖拽停止定时器 -----
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // ---------- 关闭定时器 ------------//
    [self removeTimer];
    
}

#pragma ---- 停止拖拽时开启开启定时器 ----
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // --------- 开启定时器 -----------//
    [self addTimer];
    
}

// 添加计时器
- (void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(time:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    self.timer = timer;
}

// 移除计时器
- (void)removeTimer
{
    // 将NSTimer释放
    [self.timer invalidate];
    // 置空
    self.timer = nil;
}

#pragma ---- 定时器方法 ----
- (void)time:(NSTimer *)timer
{
    if (self.scrollArr) {
        
//        NSLog(@"偏移量 ----- %g",(self.collectionView.contentOffset.x + WIDTHVIEW) / WIDTHVIEW);
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(self.collectionView.contentOffset.x / WIDTHVIEW + 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    }

    if (self.collectionView.contentOffset.x >= WIDTHVIEW * (self.scrollArr.count - 2)){
        
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        
    }
    
    if (self.pageControl.currentPage == self.scrollArr.count - 3) {
        
        self.pageControl.currentPage = 0;
        
    }else{
        
        self.pageControl.currentPage++;
        
    }
}


@end
